classdef amcinst < handle
    %AMINST Summary of this class goes here
    %   Detailed explanation goes here
    
    properties
        channels;
        instname;
        rackname;
        commobj;
    end
    
    methods
        function ami=amcinst(commtype,addrstr,rackname)
            switch commtype
                case 'gpib'
                    if isempty(str2double(addrstr))
                        addrstr='0';
                    end
                    ami.commobj=gpib('ni',0,str2double(addrstr));
                otherwise
            end
            stack=dbstack;
            filestring=stack(2).name; %find name of driver that called this constructor
            ami.instname=filestring(4:end); %remove 'ami' prefix
            ami.rackname=rackname;
            %fopen(ami.commobj);
        end
        %% return instname+fullname
        function fn=fullname(ami) %note this produces a cell-array output with vector input
            fn=strcat({ami.instname},{' '},{ami.rackname});
        end
        %% change the communication address
        function setaddress(ami,addrstr)
            if isa(ami.commobj,'gpib')
                ami.commobj.PrimaryAddress=str2double(addrstr);
            else
            end
        end
        %% get the address (as a string)
        function addrstr=address(ami)
            if isa(ami.commobj,'gpib')
                addrstr=num2str(ami.commobj.PrimaryAddress);
            else
                addrstr='none';
            end
        end
        %% destructor
        function delete(ami)
            %fclose(ami.commobj);
            global amrack;
            delete(amrack.rackchans(amrack.rackchans.inst==ami));
            amrack.insts(amrack.insts==ami)=[];
        end
    end
    
end

